package cn.etynit.org.service.impl;
import cn.etynit.basic.util.PageList;
import cn.etynit.org.domain.SDdetail;
import cn.etynit.org.domain.SDtype;
import cn.etynit.org.mapper.SDdetailMapper;
import cn.etynit.org.mapper.SDtypeMapper;
import cn.etynit.org.query.SDtypeQuery;
import cn.etynit.org.service.SDtypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class SDtypeServiceImpl implements SDtypeService {

    @Autowired
    private SDdetailMapper sDdetailMapper;

    @Autowired
    private SDtypeMapper sDtypeMapper;

    @Override
    public List<SDtype> findAll() {
        return sDtypeMapper.findAll();
    }

    @Override
    @Transactional
    public void delete(Long id) {
        List<SDdetail> list = sDdetailMapper.findAll();
        for (SDdetail sDdetail : list) {
            // 判断当前遍历到的数据的类型id是否等于传入的参数id
            if(sDdetail.getTypesId() == id){
                sDdetailMapper.delete(sDdetail.getTypesId());
            }
        }
        sDtypeMapper.delete(id);
    }

    @Override
    @Transactional
    public void batchDel(List<Long> ids) {
        sDtypeMapper.batchDel(ids);
    }

    @Override
    @Transactional
    public void add(SDtype sDtype) {
        sDtypeMapper.add(sDtype);
    }

    @Override
    @Transactional
    public void update(SDtype sDtype) {
        sDtypeMapper.update(sDtype);
    }

    @Override
    public List<SDdetail> findById(Long id) {
        return sDtypeMapper.findById(id);
    }

    @Override
    public PageList<SDtype> queryByPage(SDtypeQuery sDtypeQuery) {
        // 查询条数
        Integer totals = sDtypeMapper.queryByCount(sDtypeQuery);
        if (null == totals || 0 == totals){
            return new PageList<>(0,new ArrayList<>());
        }
        // 查询数据
        List<SDtype> sDtypes = sDtypeMapper.queryByPage(sDtypeQuery);
        return new PageList<>(totals,sDtypes);
    }
}
